package com.controller;

import com.annotation.IgnoreAuth;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.KaoyanziliaoEntity;
import com.entity.view.KaoyanziliaoView;
import com.service.KaoyanziliaoService;
import com.service.StoreupService;
import com.utils.MPUtil;
import com.utils.PageUtils;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * 考研资料
 * 后端接口
 * 赵润波 2023.5.25
 */
@RestController
@RequestMapping("/kaoyanziliao")
public class KaoyanziliaoController {
    @Autowired
    private KaoyanziliaoService kaoyanziliaoService;

    @Autowired
    private StoreupService storeupService;
    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, KaoyanziliaoEntity kaoyanziliao,
                  HttpServletRequest request){
        EntityWrapper<KaoyanziliaoEntity> ew = new EntityWrapper<KaoyanziliaoEntity>();

		PageUtils page = kaoyanziliaoService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, kaoyanziliao), params), params));

        return R.ok().put("data", page);
    }
    
    /**
     * 前端列表
     */
	@IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, KaoyanziliaoEntity kaoyanziliao,
                  HttpServletRequest request){
        EntityWrapper<KaoyanziliaoEntity> ew = new EntityWrapper<KaoyanziliaoEntity>();
        System.out.println(kaoyanziliao);
		PageUtils page = kaoyanziliaoService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, kaoyanziliao), params), params));
        return R.ok().put("data", page);
    }

	/**
     * 列表
     */
    @RequestMapping("/lists")
    public R list(KaoyanziliaoEntity kaoyanziliao){
       	EntityWrapper<KaoyanziliaoEntity> ew = new EntityWrapper<KaoyanziliaoEntity>();
      	ew.allEq(MPUtil.allEQMapPre( kaoyanziliao, "kaoyanziliao"));
        return R.ok().put("data", kaoyanziliaoService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(KaoyanziliaoEntity kaoyanziliao){
        EntityWrapper< KaoyanziliaoEntity> ew = new EntityWrapper< KaoyanziliaoEntity>();
 		ew.allEq(MPUtil.allEQMapPre( kaoyanziliao, "kaoyanziliao"));
		KaoyanziliaoView kaoyanziliaoView =  kaoyanziliaoService.selectView(ew);
        System.out.println(kaoyanziliao);
		return R.ok("查询考研资料成功").put("data", kaoyanziliaoView);
    }
	
    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        KaoyanziliaoEntity kaoyanziliao = kaoyanziliaoService.selectById(id);
        return R.ok().put("data", kaoyanziliao);
    }

    /**
     * 前端详情
     */
	@IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        KaoyanziliaoEntity kaoyanziliao = kaoyanziliaoService.selectById(id);
        return R.ok().put("data", kaoyanziliao);
    }
    


    /**
     * 赞或踩
     */
    @RequestMapping("/thumbsup/{id}")
    public R vote(@PathVariable("id") String id, String type){
        KaoyanziliaoEntity kaoyanziliao = kaoyanziliaoService.selectById(id);
        if(type.equals("1")) {
        	kaoyanziliao.setThumbsupnum(kaoyanziliao.getThumbsupnum()+1);
        } else {
        	kaoyanziliao.setCrazilynum(kaoyanziliao.getCrazilynum()+1);
        }
        kaoyanziliaoService.updateById(kaoyanziliao);
        return R.ok("投票成功");
    }

    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody KaoyanziliaoEntity kaoyanziliao, HttpServletRequest request){
    	kaoyanziliao.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
    	//ValidatorUtils.validateEntity(kaoyanziliao);
        kaoyanziliaoService.insert(kaoyanziliao);
        return R.ok();
    }
    
    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody KaoyanziliaoEntity kaoyanziliao, HttpServletRequest request){
    	kaoyanziliao.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
    	//ValidatorUtils.validateEntity(kaoyanziliao);
        kaoyanziliaoService.insert(kaoyanziliao);
        return R.ok();
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody KaoyanziliaoEntity kaoyanziliao, HttpServletRequest request){
        //ValidatorUtils.validateEntity(kaoyanziliao);
        kaoyanziliaoService.updateById(kaoyanziliao);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        kaoyanziliaoService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }

}
